/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.lczyfy.idcp.modules.crawl.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.lczyfy.idcp.common.persistence.Page;
import com.lczyfy.idcp.common.service.CrudService;
import com.lczyfy.idcp.common.utils.StringUtils;
import com.lczyfy.idcp.modules.crawl.entity.CrawlWebMain;
import com.lczyfy.idcp.modules.crawl.dao.CrawlWebMainDao;
import com.lczyfy.idcp.modules.crawl.entity.CrawlWebChild;
import com.lczyfy.idcp.modules.crawl.dao.CrawlWebChildDao;

/**
 * 爬虫目标Service
 * @author cw
 * @version 2018-07-20
 */
@Service
@Transactional(readOnly = true)
public class CrawlWebMainService extends CrudService<CrawlWebMainDao, CrawlWebMain> {

	@Autowired
	private CrawlWebChildDao crawlWebChildDao;
	
	public CrawlWebMain get(String id) {
		CrawlWebMain crawlWebMain = super.get(id);
		crawlWebMain.setCrawlWebChildList(crawlWebChildDao.findList(new CrawlWebChild(crawlWebMain)));
		return crawlWebMain;
	}
	
	public List<CrawlWebMain> findList(CrawlWebMain crawlWebMain) {
		return super.findList(crawlWebMain);
	}
	
	public Page<CrawlWebMain> findPage(Page<CrawlWebMain> page, CrawlWebMain crawlWebMain) {
		return super.findPage(page, crawlWebMain);
	}
	
	@Transactional(readOnly = false)
	public void save(CrawlWebMain crawlWebMain) {
		super.save(crawlWebMain);
		for (CrawlWebChild crawlWebChild : crawlWebMain.getCrawlWebChildList()){
			if (crawlWebChild.getId() == null){
				continue;
			}
			if (CrawlWebChild.DEL_FLAG_NORMAL.equals(crawlWebChild.getDelFlag())){
				if (StringUtils.isBlank(crawlWebChild.getId())){
					crawlWebChild.setWebMain(crawlWebMain);
					crawlWebChild.preInsert();
					crawlWebChild.setStat("0");
					crawlWebChildDao.insert(crawlWebChild);
				}else{
					crawlWebChild.preUpdate();
					crawlWebChildDao.update(crawlWebChild);
				}
			}else{
				crawlWebChildDao.delete(crawlWebChild);
			}
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(CrawlWebMain crawlWebMain) {
		super.delete(crawlWebMain);
		crawlWebChildDao.delete(new CrawlWebChild(crawlWebMain));
	}
	
}